#!/bin/bash

# Minidis本地功能测试脚本
echo "=== Minidis 本地功能测试 ==="

HOST="127.0.0.1"
PORT="6379"

# 发送 RESP 命令的函数
send_command() {
    echo -e "$1" | nc $HOST $PORT
}

echo "1. 测试基础连接..."
send_command '*1\r\n$4\r\nPING\r\n'

echo -e "\n2. 测试字符串操作..."
send_command '*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n'
send_command '*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n'

echo -e "\n3. 测试 APPEND 命令..."
send_command '*3\r\n$6\r\nAPPEND\r\n$5\r\nhello\r\n$1\r\n!\r\n'
send_command '*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n'

echo -e "\n4. 测试计数器..."
send_command '*3\r\n$3\r\nSET\r\n$7\r\ncounter\r\n$1\r\n0\r\n'
send_command '*2\r\n$4\r\nINCR\r\n$7\r\ncounter\r\n'
send_command '*2\r\n$4\r\nINCR\r\n$7\r\ncounter\r\n'
send_command '*2\r\n$3\r\nGET\r\n$7\r\ncounter\r\n'

echo -e "\n5. 测试列表操作..."
send_command '*4\r\n$5\r\nLPUSH\r\n$6\r\nmylist\r\n$5\r\nitem1\r\n$5\r\nitem2\r\n'
send_command '*2\r\n$4\r\nLLEN\r\n$6\r\nmylist\r\n'
send_command '*4\r\n$6\r\nLRANGE\r\n$6\r\nmylist\r\n$1\r\n0\r\n$2\r\n-1\r\n'

echo -e "\n6. 测试哈希操作..."
send_command '*4\r\n$4\r\nHSET\r\n$6\r\nuser:1\r\n$4\r\nname\r\n$5\r\nAlice\r\n'
send_command '*4\r\n$4\r\nHSET\r\n$6\r\nuser:1\r\n$3\r\nage\r\n$2\r\n30\r\n'
send_command '*3\r\n$4\r\nHGET\r\n$6\r\nuser:1\r\n$4\r\nname\r\n'
send_command '*2\r\n$7\r\nHGETALL\r\n$6\r\nuser:1\r\n'

echo -e "\n7. 测试集合操作..."
send_command '*4\r\n$4\r\nSADD\r\n$5\r\nmyset\r\n$5\r\napple\r\n$6\r\nbanana\r\n'
send_command '*2\r\n$5\r\nSCARD\r\n$5\r\nmyset\r\n'
send_command '*2\r\n$8\r\nSMEMBERS\r\n$5\r\nmyset\r\n'

echo -e "\n8. 测试通用命令..."
send_command '*2\r\n$4\r\nKEYS\r\n$1\r\n*\r\n'
send_command '*2\r\n$4\r\nTYPE\r\n$5\r\nhello\r\n'
send_command '*3\r\n$6\r\nEXISTS\r\n$5\r\nhello\r\n$10\r\nnonexistent\r\n'

echo -e "\n9. 测试服务器命令..."
send_command '*2\r\n$4\r\nECHO\r\n$13\r\nHello Minidis!\r\n'
send_command '*1\r\n$4\r\nTIME\r\n'
send_command '*1\r\n$4\r\nINFO\r\n'

echo -e "\n=== 测试完成 ==="